package com.zsk.shop.common.base.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.utils.HttpContext;
import com.zsk.shop.common.utils.ToolUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Layui Table默认的分页参数创建
 */
public class BasePageFactory {
    /**
     * 获取layui table的分页参数
     *
     * @Date 2019/1/25 22:13
     */
    public static Page defaultPage() {
        HttpServletRequest request = HttpContext.getRequest();
        String limits = request.getParameter("limit");
        String pages = request.getParameter("page");
        if (ToolUtil.isEmpty(limits)) {
            limits = "10";
        }
        if (ToolUtil.isEmpty(pages)) {
            pages = "1";
        }
        //每页多少条数据
        int limit = Integer.valueOf(limits);
        //第几页
        int page = Integer.valueOf(pages);
        return new Page(page, limit);
    }

    public static Page defaultPage(String pages, String limits) {
        if (ToolUtil.isEmpty(limits)) {
            limits = "10";
        }
        if (ToolUtil.isEmpty(pages)) {
            pages = "1";
        }
        //每页多少条数据
        int limit = Integer.valueOf(limits);
        //第几页
        int page = Integer.valueOf(pages);
        return new Page(page, limit);
    }

    public static int PagePage() {
        HttpServletRequest request = HttpContext.getRequest();
        String pages = request.getParameter("page");
        if (ToolUtil.isEmpty(pages)) {
            pages = "1";
        }
        int page = Integer.valueOf(pages);
        return page;
    }

    /**
     * 获取layui table的分页参数
     *
     * @Date 2019/1/25 22:13
     */
    public static Page exportPage() {
        //每页多少条数据
        int limit = 100000;
        //第几页
        int page = 1;
        return new Page(page, limit);
    }

    /**
     * 创建layui能识别的分页响应参数
     */
    public static BaseResponse createPageInfo(IPage page) {
        BaseResponse result = new BaseResponse();
        result.setCount(page.getTotal());
        result.setData(page.getRecords());
        if (page.getRecords() == null) {
            result.setData(new ArrayList());
        }
        return result;
    }

    /**
     * 创建layui能识别的分页响应参数 ext增加拓展
     */
    public static BaseResponse createPageInfoWithExt(IPage page, Object ext) {
        BaseResponse result = new BaseResponse();
        result.setCount(page.getTotal());
        result.setData(page.getRecords());
        result.setExt(ext);
        return result;
    }


    /**
     * 创建layui能识别的分页响应参数
     */
    public static <T> BaseResponse createPageByList(List<T> list, long total) {
        BaseResponse result = new BaseResponse();
        result.setCount(total);
        result.setData(list);
        return result;
    }
}
